<?php
namespace  common\modules\user\backend\models;

use common\modules\user\models\User;
use yii\base\Model;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * Create user form
 */
class UserForm extends Model
{

    public $username;

    public $email;

    public $password;
    
    public $source;

    public $status;

    public $roles;

    private $model;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                'username',
                'filter',
                'filter' => 'trim'
            ],
            [
                'username',
                'required'
            ],
            [
                'username',
                'unique',
                'targetClass' => '\common\modules\user\models\User',
                'filter' => function ($query)
                {
                    if (! $this->getModel()->isNewRecord) {
                        $query->andWhere([
                            'not',
                            [
                                'id' => $this->getModel()->id
                            ]
                        ]);
                    }
                }
            ],
            [
                'username',
                'string',
                'min' => 2,
                'max' => 255
            ],
            
            [
                'email',
                'filter',
                'filter' => 'trim'
            ],
            [
                'email',
                'required'
            ],
            [
                'email',
                'email'
            ],
            [
                'email',
                'unique',
                'targetClass' => '\common\modules\user\models\User',
                'filter' => function ($query)
                {
                    if (! $this->getModel()->isNewRecord) {
                        $query->andWhere([
                            'not',
                            [
                                'id' => $this->getModel()->id
                            ]
                        ]);
                    }
                }
            ],
            
            [
                'password',
                'required',
                'on' => 'create'
            ],
            [
                'password',
                'string',
                'min' => 6
            ],
            [
                'source',
                'each',
                'rule' => [
                    'in',
                    'range' => [User::SOURCE_FRONTEND, User::SOURCE_BACKEND]
                ]
            ],
            [
                [
                    'status'
                ],
                'boolean'
            ],
            [
                [
                    'roles'
                ],
                'each',
                'rule' => [
                    'in',
                    'range' => ArrayHelper::getColumn(Yii::$app->authManager->getRoles(), 'name')
                ]
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username' => Yii::t('common', 'Username'),
            'email' => Yii::t('common', 'Email'),
            'password' => Yii::t('common', 'Password'),
            'source' => '用户来源',
            'roles' => Yii::t('backend', 'Roles')
        ];
    }

    public function setModel($model)
    {
        $this->username = $model->username;
        $this->email = $model->email;
        $this->source = $model->source;
        $this->status = $model->status;
        $this->model = $model;
        $this->roles = ArrayHelper::getColumn(Yii::$app->authManager->getRolesByUser($model->getId()), 'name');
        return $this->model;
    }

    public function getModel()
    {
        if (! $this->model) {
            $this->model = new User();
        }
        return $this->model;
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function save()
    {
        if ($this->validate()) {
            $model = $this->getModel();
            $isNewRecord = $model->getIsNewRecord();
            $model->username = $this->username;
            $model->email = $this->email;
            $model->status = $this->status;
            if ($this->password) {
                $model->setPassword($this->password);
            }
            if ($model->save() && $isNewRecord) {
                $model->afterSignup();
            }
            $auth = Yii::$app->authManager;
            $auth->revokeAll($model->getId());
            
            if ($this->roles && is_array($this->roles)) {
                foreach ($this->roles as $role) {
                    $auth->assign($auth->getRole($role), $model->getId());
                }
            }
            
            return ! $model->hasErrors();
        }
        return null;
    }
}
